/*
 * adc_usart.c
 *
 *  Created on: Dec 7, 2012
 *      Author: andrey
 */

#include "adc_usart.h"
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_usart.h>

#ifndef PAL_PORT_BIT
#define PAL_PORT_BIT(n) ((1 << (n)))
#endif

#define ADC_USART						USART2

#define ADC_USART_PORT					GPIOD

void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);

void initAdcUsart() {

	USART_InitTypeDef USART_InitStructure;
	/* USARTx configured as follow:
	 - BaudRate = 38400 baud
	 - Word Length = 8 Bits
	 - One Stop Bit
	 - No parity
	 - Hardware flow control disabled (RTS and CTS signals)
	 - Receive and transmit enabled
	 */
	USART_InitStructure.USART_BaudRate = 62500;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl =
			USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	GPIO_InitTypeDef GPIO_InitStructure;

	/* Enable GPIO clock */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

	//  if (CONSOLE_USART != USART3)
	{
		/* Enable UART clock */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	}

	/* Connect PXx to USARTx_Tx*/
	GPIO_PinAFConfig(ADC_USART_PORT, 5, 7);

	/* Connect PXx to USARTx_Rx*/
	GPIO_PinAFConfig(ADC_USART_PORT, 6, 7);

	/* Connect CKx to USARTx_Tx*/
	GPIO_PinAFConfig(ADC_USART_PORT, 7, 7);

	/* Configure USART Tx as alternate function  */
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

	GPIO_InitStructure.GPIO_Pin = PAL_PORT_BIT(5);
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(ADC_USART_PORT, &GPIO_InitStructure);

	/* Configure USART Rx as alternate function  */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = PAL_PORT_BIT(6);
	GPIO_Init(ADC_USART_PORT, &GPIO_InitStructure);

	/* Configure USART CK as alternate function  */
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

	GPIO_InitStructure.GPIO_Pin = PAL_PORT_BIT(7);
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(ADC_USART_PORT, &GPIO_InitStructure);

	USART_ClockInitTypeDef USART_ClockInitStructure;
	USART_ClockStructInit(&USART_ClockInitStructure);
	USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
	USART_ClockInit(USART2, &USART_ClockInitStructure);

//	  USARTx_CLK_INIT();

	/* USART configuration */
	USART_Init(ADC_USART, &USART_InitStructure);

	/* Enable USART */
	USART_Cmd(ADC_USART, ENABLE);
}

void stmPutAdc(int ch) {
	/* write a character to the USART */
	USART_SendData(ADC_USART, (uint8_t) ch);

	/* Loop until the end of transmission */
	while (USART_GetFlagStatus(ADC_USART, USART_FLAG_TC ) == RESET) {
	}
}

//void adcUsartPoke() {
////	stmPutAdc(249);		// 11111001b, in real L HLLH HHHH
////	stmPutAdc(6);		// 00000110b, in real L LHHL LLLL
////	stmPutAdc(71);
//	//stmPutAdc(0);
////	stmPutAdc(71); 		// 01000111b, in real L HHHL LLHL
////	stmPutAdc(255);
////	stmPutAdc(191);
//
////	stmPutAdc(192); 	// 11000000b, in real L LLLL LLHH
//
////	stmPutAdc(253);
//	stmPutAdc(255);
//}

int result0 = 0;
int result2 = 0;
int callC = 0;
int adcGetvalue() {
	if (USART_GetFlagStatus(ADC_USART, USART_FLAG_RXNE ) == RESET)
		return 20; // no data pending
	callC++;
	int result = USART_ReceiveData(ADC_USART );
	if (callC % 2 == 0)
		result0 = result;
	else
		result2 = result;

	return result0;
}

int adcGetvalue2() {
	return result2;

}
